package baoxiu.controller;

import baoxiu.entity.Building;
import baoxiu.entity.BulidingDetail;
import baoxiu.entity.Goods;
import baoxiu.service.BuildingService;
import baoxiu.service.GoodsService;
import baoxiu.vo.DropDown;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.util.Res;
import common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName GoodsController
 * @Author mengjiafeng
 * @Date 2019/7/24 20:29
 **/
@RestController
@RequestMapping("/baoxiu/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private BuildingService buildingService;
    @GetMapping("/findAll")
    public Result selectAll(Goods goods, Page page){
        return Res.makeOKRsp(goodsService.page(page,new QueryWrapper<Goods>().like("name",goods.getName())));
    }
    @GetMapping("/findAllByType")
    public Result findAllByType(String buildType){
        List<Goods> goodsList = goodsService.list(new QueryWrapper<Goods>().eq("build_type", buildType));
//                .stream().map(item->{
//            DropDown dropDown = new DropDown();
//            dropDown.setLabel(item.getName());
//            dropDown.setValue(item.getId());
//            return dropDown;
//        }).collect(Collectors.toList());
        return Res.makeOKRsp(goodsList);
    }

    @PostMapping("/add")
    public Result add(Goods goods){
        Building building = buildingService.getById(goods.getBuildType());
        goods.setTypeName(building.getName());
        goodsService.save(goods);
        return Res.makeOKRsp("添加成功");
    }
    @GetMapping("/delete")
    public Result delete(String id){
        goodsService.removeById(id);
        return Res.makeOKRsp("删除成功");

    }
}
